﻿using SketchUpAPI.NET.Objects;
using SketchUpAPI.NET.SketchUp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpConsole.Examples
{
    /// <summary>
    /// 组件定义
    /// </summary>
    public static class CreateComponentDefinition
    {
        public static void Run()
        {
            string writePath = @"ExampleFiles\1.skp";
            string read2022 = @"ExampleFiles\2022.skp";
            string read2 = @"ExampleFiles\2.skp";

            SketchUpApplication.Initialize();
            SketchUpModel skp = SketchUpModel.Create();
            var ct = skp.ComponentDefinitions;

            // 定义组件
            ComponentDefinition comp = new ComponentDefinition();
            ct.Add(comp);

            EntityTable et = comp.Entities;

            GeometryInput geometry = new GeometryInput();

            Point3D[] points = new Point3D[]
            {
                new Point3D(0, 0, 0),
                new Point3D(100.0.ToModel(), 0, 0),
                new Point3D(100.0.ToModel(), 100.0.ToModel(), 0),
            };

            geometry.AddFace(points);
            et.Add(geometry, true);

            // 创建组件实例
            ComponentInstance ci = comp.CreateInstance();
            skp.Entities.Add(ci); // 将实例添加的skp模型中

            skp.Save(writePath);
            skp.Dispose();
            SketchUpApplication.Terminate();
        }
    }
}